/*
 * ============================================================================
 * Copyright Notice!
 * ----------------------------------------------------------------------------
 * This file contains proprietary and confidential information belonging to the
 * group of people involvled in developing this project.  Reproduction, disclosure 
 * or use without specific written authorization from project stake holders is 
 * prohibited. Please refer the readme.txt in project installation folder for more
 * specific contacts
 *
 * All rights reserved.
 * ============================================================================
 */
package edu.uwm.universitydb.framework.transactional.db.command.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import edu.uwm.universitydb.framework.db.command.DataFetchCommand;
import edu.uwm.universitydb.framework.exception.DataAccessErrorCode;
import edu.uwm.universitydb.framework.exception.DataAccessException;

/**
 * <code>TransactionalDBFetchCommandBase</type>
 *  Base class for data fetch command to be used with in a transactional context..
 *  Will be used very rarely.. like in upsert scenarios
 * 
 * @author Shyam Nair
 */
public abstract class TransactionalDBFetchCommandBase<T> extends TransactionalDBCommandBase<T>
    implements DataFetchCommand<T>
{

    /*
     * (non-javadoc)
     * @see edu.uwm.universitydb.framework.transactional.db.command.TransactionalDBCommand#executeCommand(java.sql.Connection)
     */
    @Override
    public T executeCommand(final Connection dbConnection) throws DataAccessException
    {
        // create statement and result set
        PreparedStatement pStmt = null;
        ResultSet result = null;
        try
        {
            final String sql = getSQL();
            pStmt = dbConnection.prepareStatement(sql);
            setParameters(pStmt);
            result = pStmt.executeQuery();
            return processResult(result);
        }
        catch (final SQLException sqlEx)
        {
            sqlEx.printStackTrace();
            final String eMessage = sqlEx.getMessage();
            throw new DataAccessException(DataAccessErrorCode.UNCATEGAROZIED_ERROR, eMessage, sqlEx);
        }
        finally
        {
            if (pStmt != null)
            {
                try
                {
                    pStmt.close();
                    result.close();
                }
                catch (final SQLException e)
                {
                    // DO NOTHING.. JUST LOG
                    System.out
                        .println(" Exception while closing Prepared Stmt : " + e.getMessage());
                    e.printStackTrace();
                }
            }
        }
    }

}
/*
 * ============================================================================
 * Change History
 * ----------------------------------------------------------------------------
 * Apr 19, 2014   Shyam Nair
 * File created
 * ----------------------------------------------------------------------------
 */